/*
* Copyright 2018 Shanxi Lu'an Taihang Laihui Net Tech co.,LTD.
*/
package com.laihuichuxing.mq.consumerRocket;

import com.laihuichuxing.mq.constant.DriverPositionConst;
import com.laihuichuxing.mq.dao.PositionDao;
import com.laihuichuxing.order.dao.PassengerOrderDao;
import com.laihuichuxing.order.util.NettyConst;
import com.laihuichuxing.protobuf.module.OrderModule;
import com.laihuichuxing.socket.ModuleId;
import com.laihuichuxing.socket.NettyClient;
import com.laihuichuxing.socket.Request;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.starter.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.starter.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Description:
 *
 * @author: Qinxuefeng
 * @since: 1.0
 * Create Date Time: 2018/10/29 12:01
 * Update Date Time:
 */
@Slf4j
@Service
@RocketMQMessageListener(topic = "driverDepartureTopic", consumerGroup = "${spring.rocketmq.driverDepartureGroup}")
public class DriverDepartureRConsumer implements RocketMQListener<Map> {

    @Autowired
    private PositionDao positionDao;

    @Autowired
    private PassengerOrderDao passengerOrderDao;

    @Autowired
    private NettyConst nettyConst;

    /**
     * Description: 司机发车topic
     *
     * @param map
     * @author: YangGuang
     * Create Date Time: 2018/10/18 10:35
     * Update Date Time:
     */
    @Override
    public void onMessage(Map map) {
        try {
            // 获取数据
            String longitude = map.get("longitude").toString();
            String latitude = map.get("latitude").toString();
            String mobile = map.get("mobile").toString();
            String cityCode = map.get("cityCode").toString();
            String orderId = map.get("orderId").toString();
            // 保存车主位置
            positionDao.insertPosition(mobile, longitude, latitude, cityCode, DriverPositionConst.DEPARTURE_POSITION, DriverPositionConst.DRIVER_TABLE);
            // 通知乘客
            this.putNetty2NotifyPassengerDeparture(orderId);
        } catch (Exception e) {
            log.error("司机发车mq异常:{}", e.toString(), e);
        }
    }

    /**
     * Description: 长连接通知乘客司机发车
     *
     * @param:
     * @return:
     * @author: YangGuang
     * Create Date Time: 2018/10/18 10:35
     * Update Date Time:
     */
    private void putNetty2NotifyPassengerDeparture(String orderId) {
        // 查询乘客手机号
        String passengerMobile = passengerOrderDao.selectMobileByOrderId(orderId);
        // 序列化数据
        OrderModule.Order order = OrderModule.Order.newBuilder().setOrderId(orderId).setPassengerMobile(passengerMobile).build();
        // 封装请求
        Request request = Request.valueOf(ModuleId.LBS, (short) 12, order.toByteArray());
        // 初始化客户端
        NettyClient nettyClient = new NettyClient(nettyConst.getAddress(), nettyConst.getPort());
        // 连接长连接
        try {
            nettyClient.connect();
            // 给客户端发消息
            nettyClient.sendRequest(request);
        } catch (InterruptedException e) {
            log.error("司机发车长连接异常:{}", e.toString(), e);
        } finally {
            // 关闭连接
            nettyClient.shutdown();
            Thread.currentThread().interrupt();
        }
    }
}
